package com.example.suze.libraryinformationmanagementsystem.Activity;

import android.content.Intent;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.suze.libraryinformationmanagementsystem.R;
import com.example.suze.libraryinformationmanagementsystem.net.API;
import com.example.suze.libraryinformationmanagementsystem.net.JSONCallBack;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.ReturnStateEntity;

public class LoginActivity extends BaseActivity {
    private EditText nameV;
    private EditText passwordV;
    private Button login;
    private String name;
    private String password;
    private JSONCallBack callBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        findView();
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = nameV.getText().toString();
                password = passwordV.getText().toString();
                callBack = new JSONCallBack<ReturnStateEntity>(new ReturnStateEntity()) {
                    @Override
                    public void onSuccess(ReturnStateEntity entity) {
                        Log.v("login", entity.getState()+" "+entity.getCode());
                        if(entity.getState().equals("success") && entity.getCode() == 0){
                            turnToMainActivity();
                        }else{
                            Looper.prepare();
                            Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_LONG).show();
                            Looper.loop();
                        }
                    }
                };
                api.login(name, password, callBack);
            }
        });//请求服务器判断用户和密码

    }

    void turnToMainActivity(){
        startActivity(new Intent(LoginActivity.this,MainActivity.class));
        finish();
    }

    private void findView(){
        nameV = (EditText) findViewById(R.id.name);
        passwordV = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
    }


}
